package com.mbengue.corp.artisan.vue.tableau;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.mbengue.corp.artisan.utils.Outil;

public class Client extends JTable {
  private static final long serialVersionUID = 1455306497698390839L;
  private int nbLigne = 0;
  private Map<String, Object[]> donnees;

  public Client() {
    super();

    this.donnees = new HashMap<String, Object[]>();
    TableModel modele = new DefaultTableModel(Outil.EN_TETE_CLIENT, nbLigne);
    this.setModel(modele);
    this.setVisible(true);
  }

  public void rechargerFeuille() {
    TableModel modele = new DefaultTableModel(Outil.EN_TETE_CLIENT, nbLigne);
    Set<String> numeros = donnees.keySet();
    int ligne = 0;
    for (String numero : numeros) {
      for (int col = 0; col < Outil.EN_TETE_CLIENT.length; col++) {
        Object valeur = donnees.get(numero)[col];
        modele.setValueAt(valeur, ligne, col);
      }
      ligne++;
    }
    this.setModel(modele);
    this.centrerTexte();
    this.setVisible(true);
  }

  public Map<String, Object[]> getDonnees() {
    return this.donnees;
  }

  public void setDonnees(Map<String, Object[]> donnees) {
    this.donnees = donnees;
    this.nbLigne = this.donnees.size();
  }

  public void ajouterDonnee(String code, Object[] donnee) {
    this.donnees.put(code, donnee);
    this.nbLigne++;
  }

  private void centrerTexte() {
    DefaultTableCellRenderer custom = new DefaultTableCellRenderer();
    custom.setHorizontalAlignment(SwingConstants.CENTER);
    for (int i = 0; i < this.getColumnCount(); i++) {
      this.getColumnModel().getColumn(i).setCellRenderer(custom);
    }
  }
}
